// 卡片数据 - 根据您的图片命名规则
const eventCards = [
    "images/chance-02.png",
    "images/chance-03.png",
    "images/chance-04.png",
    "images/chance-05.png",
    "images/chance-06.png",
    "images/chance-07.png",
    "images/chance-08.png",
    "images/chance-09.png",
    "images/chance-10.png",
    "images/chance-11.png",
    "images/chance-12.png",
    "images/chance-13.png",
    "images/chance-14.png",
    "images/chance-15.png",
    "images/chance-16.png",
    "images/chance-17.png"
];

const skillCards = [
    "images/SKILL-02.png",
    "images/SKILL-03.png",
    "images/SKILL-04.png",
    "images/SKILL-05.png",
    "images/SKILL-06.png",
    "images/SKILL-07.png",
    "images/SKILL-08.png",
    "images/SKILL-09.png",
    "images/SKILL-10.png",
    "images/SKILL-11.png",
    "images/SKILL-12.png",
    "images/SKILL-13.png"
];

// 页面元素
const homePage = document.getElementById('home-page');
const eventDrawPage = document.getElementById('event-draw-page');
const skillDrawPage = document.getElementById('skill-draw-page');
const eventCardElement = document.getElementById('event-card');
const skillCardElement = document.getElementById('skill-card');
const eventHistoryCardElement = document.getElementById('event-history-card');
const skillHistoryCardElement = document.getElementById('skill-history-card');
const eventFrontImg = document.getElementById('event-front-img');
const skillFrontImg = document.getElementById('skill-front-img');
const eventHistoryImg = document.getElementById('event-history-img');
const skillHistoryImg = document.getElementById('skill-history-img');
const eventHistoryBtn = document.getElementById('event-history-btn');
const skillHistoryBtn = document.getElementById('skill-history-btn');
const eventHistoryColumn = document.getElementById('event-history-column');
const skillHistoryColumn = document.getElementById('skill-history-column');

// 状态变量
let eventDrawCount = 0;
let skillDrawCount = 0;
let currentEventCard = null;
let currentSkillCard = null;
let previousEventCard = null;
let previousSkillCard = null;
let isEventCardFlipped = false;
let isSkillCardFlipped = false;
let isEventHistoryCardFlipped = false;
let isSkillHistoryCardFlipped = false;
let isShowingHistoryEvent = false;
let isShowingHistorySkill = false;

// 初始化事件监听
function init() {
    // 首页按钮事件
    document.getElementById('event-card-option').addEventListener('click', () => {
        showPage('event');
        // 重置事件卡状态
        resetCardState('event');
    });
    
    document.getElementById('skill-card-option').addEventListener('click', () => {
        showPage('skill');
        // 重置技能卡状态
        resetCardState('skill');
    });
    
    // 抽卡按钮事件
    document.getElementById('draw-event-btn').addEventListener('click', drawEventCard);
    document.getElementById('draw-skill-btn').addEventListener('click', drawSkillCard);
    
    // 查看历史按钮事件
    eventHistoryBtn.addEventListener('click', showLastEventCard);
    skillHistoryBtn.addEventListener('click', showLastSkillCard);
    
    // 卡片点击事件 - 点击卡片可以翻转
    eventCardElement.addEventListener('click', () => {
        if (isEventCardFlipped) {
            resetCard(eventCardElement, 'event');
        } else if (eventFrontImg.src) {
            flipCard(eventCardElement, 'event');
        }
    });
    
    eventHistoryCardElement.addEventListener('click', () => {
        if (isEventHistoryCardFlipped) {
            resetCard(eventHistoryCardElement, 'event-history');
        } else if (eventHistoryImg.src) {
            flipCard(eventHistoryCardElement, 'event-history');
        }
    });
    
    skillCardElement.addEventListener('click', () => {
        if (isSkillCardFlipped) {
            resetCard(skillCardElement, 'skill');
        } else if (skillFrontImg.src) {
            flipCard(skillCardElement, 'skill');
        }
    });
    
    skillHistoryCardElement.addEventListener('click', () => {
        if (isSkillHistoryCardFlipped) {
            resetCard(skillHistoryCardElement, 'skill-history');
        } else if (skillHistoryImg.src) {
            flipCard(skillHistoryCardElement, 'skill-history');
        }
    });
}

// 显示对应页面
function showPage(type) {
    homePage.style.display = 'none';
    eventDrawPage.style.display = 'none';
    skillDrawPage.style.display = 'none';
    
    if (type === 'home') {
        homePage.style.display = 'block';
    } else if (type === 'event') {
        eventDrawPage.style.display = 'block';
    } else if (type === 'skill') {
        skillDrawPage.style.display = 'block';
    }
}

// 重置卡片状态
function resetCardState(type) {
    if (type === 'event') {
        resetCard(eventCardElement, 'event');
        resetCard(eventHistoryCardElement, 'event-history');
        eventHistoryColumn.style.display = 'none';
        isShowingHistoryEvent = false;
        eventHistoryBtn.disabled = true;
    } else if (type === 'skill') {
        resetCard(skillCardElement, 'skill');
        resetCard(skillHistoryCardElement, 'skill-history');
        skillHistoryColumn.style.display = 'none';
        isShowingHistorySkill = false;
        skillHistoryBtn.disabled = true;
    }
}

// 重置卡片
function resetCard(cardElement, type) {
    cardElement.classList.remove('flipped');
    if (type === 'event') {
        isEventCardFlipped = false;
    } else if (type === 'event-history') {
        isEventHistoryCardFlipped = false;
    } else if (type === 'skill') {
        isSkillCardFlipped = false;
    } else if (type === 'skill-history') {
        isSkillHistoryCardFlipped = false;
    }
}

// 翻转卡片
function flipCard(cardElement, type) {
    cardElement.classList.add('flipped');
    if (type === 'event') {
        isEventCardFlipped = true;
    } else if (type === 'event-history') {
        isEventHistoryCardFlipped = true;
    } else if (type === 'skill') {
        isSkillCardFlipped = true;
    } else if (type === 'skill-history') {
        isSkillHistoryCardFlipped = true;
    }
}

// 抽取随机事件卡
function drawEventCard() {
    // 如果正在显示历史记录，先隐藏历史列
    if (isShowingHistoryEvent) {
        hideHistory('event');
    }
    
    // 如果卡片已经翻转，先重置
    if (isEventCardFlipped) {
        resetCard(eventCardElement, 'event');
        // 延迟一下再执行抽卡，让用户看到卡片翻转回去
        setTimeout(() => {
            performEventDraw();
        }, 500);
    } else {
        performEventDraw();
    }
}

function performEventDraw() {
    // 随机选择一张事件卡
    const randomIndex = Math.floor(Math.random() * eventCards.length);
    const selectedCard = eventCards[randomIndex];
    
    // 保存上一次抽到的卡片
    if (currentEventCard) {
        previousEventCard = currentEventCard;
        // 启用查看历史按钮
        eventHistoryBtn.disabled = false;
    }
    
    // 更新当前卡片
    currentEventCard = selectedCard;
    
    // 更新当前卡片正面图片
    eventFrontImg.src = selectedCard;
    
    // 翻转当前卡片
    flipCard(eventCardElement, 'event');
    
    // 更新计数器
    eventDrawCount++;
    document.getElementById('event-counter').textContent = `已抽取: ${eventDrawCount} 次`;
}

// 抽取技能卡
function drawSkillCard() {
    // 如果正在显示历史记录，先隐藏历史列
    if (isShowingHistorySkill) {
        hideHistory('skill');
    }
    
    // 如果卡片已经翻转，先重置
    if (isSkillCardFlipped) {
        resetCard(skillCardElement, 'skill');
        // 延迟一下再执行抽卡，让用户看到卡片翻转回去
        setTimeout(() => {
            performSkillDraw();
        }, 500);
    } else {
        performSkillDraw();
    }
}

function performSkillDraw() {
    // 随机选择一张技能卡
    const randomIndex = Math.floor(Math.random() * skillCards.length);
    const selectedCard = skillCards[randomIndex];
    
    // 保存上一次抽到的卡片
    if (currentSkillCard) {
        previousSkillCard = currentSkillCard;
        // 启用查看历史按钮
        skillHistoryBtn.disabled = false;
    }
    
    // 更新当前卡片
    currentSkillCard = selectedCard;
    
    // 更新当前卡片正面图片
    skillFrontImg.src = selectedCard;
    
    // 翻转当前卡片
    flipCard(skillCardElement, 'skill');
    
    // 更新计数器
    skillDrawCount++;
    document.getElementById('skill-counter').textContent = `已抽取: ${skillDrawCount} 次`;
}

// 显示上一次抽到的事件卡
function showLastEventCard() {
    if (!previousEventCard) return;
    
    // 显示历史列
    eventHistoryColumn.style.display = 'flex';
    isShowingHistoryEvent = true;
    
    // 更新历史卡片图片
    eventHistoryImg.src = previousEventCard;
    
    // 如果历史卡片没有翻转，翻转它
    if (!isEventHistoryCardFlipped) {
        flipCard(eventHistoryCardElement, 'event-history');
    }
    
    // 确保当前卡片是正面显示
    if (!isEventCardFlipped && currentEventCard) {
        flipCard(eventCardElement, 'event');
    }
}

// 显示上一次抽到的技能卡
function showLastSkillCard() {
    if (!previousSkillCard) return;
    
    // 显示历史列
    skillHistoryColumn.style.display = 'flex';
    isShowingHistorySkill = true;
    
    // 更新历史卡片图片
    skillHistoryImg.src = previousSkillCard;
    
    // 如果历史卡片没有翻转，翻转它
    if (!isSkillHistoryCardFlipped) {
        flipCard(skillHistoryCardElement, 'skill-history');
    }
    
    // 确保当前卡片是正面显示
    if (!isSkillCardFlipped && currentSkillCard) {
        flipCard(skillCardElement, 'skill');
    }
}

// 隐藏历史列
function hideHistory(type) {
    if (type === 'event') {
        eventHistoryColumn.style.display = 'none';
        isShowingHistoryEvent = false;
    } else if (type === 'skill') {
        skillHistoryColumn.style.display = 'none';
        isShowingHistorySkill = false;
    }
}

// 初始化应用
window.onload = init;